/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Orders                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： OrdersService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreCMS.Services
{
    public class OrdersService: IOrdersService
    {
        private readonly IOrdersRepository _repository;
        private readonly IOrder_goodsRepository _order_goods;
        private readonly IArticleRepository _article;
        private readonly IArticle_goodsRepository _article_goods;
        private readonly ISite_paymentRepository _site_payment;
        public OrdersService(IOrdersRepository repository, IOrder_goodsRepository order_goods, IArticleRepository article, IArticle_goodsRepository article_goods, ISite_paymentRepository site_payment)
        {
            _repository = repository;
            _order_goods = order_goods;
            _article = article;
            _article_goods = article_goods;
            _site_payment = site_payment;
        }

        public bool Delete(int id)
        {
            return _repository.DeleteAll(id);
        }

        public bool Exists(string order_no)
        {
            return _repository.GetList("where order_no=@order_no", new { order_no = order_no }).FirstOrDefault() != null ? true : false;
        }

        public bool Exists(int id)
        {
            return GetModel(id) != null ? true : false;
        }

        public Orders GetModel(string order_no)
        {
            var orders = _repository.GetList("where order_no=@order_no", new { order_no = order_no }).FirstOrDefault();
            if(orders!=null)
            {
                var order_goods = _order_goods.GetList("where order_id=@order_id", new { order_id = orders.id }).ToList();
                if(order_goods!=null)
                {
                    orders.order_goods = order_goods;
                }
            }
            return orders;
        }

        public Orders GetModel(int id)
        {
            var orders = _repository.Get(id);
            if (orders != null)
            {
                orders.order_goods = _order_goods.GetList("where order_id=@order_id", new { order_id = orders.id }).ToList();
            }
            return orders;
        }

        public List<Models.Orders> GetList(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null)
        {
            return _repository.GetListPaged(pageNumber, rowsPerPage, conditions, orderby, parameters).ToList();
        }
        public int RecordCount(string conditions, object parameters = null)
        {
            return _repository.RecordCount(conditions, parameters);
        }

        public bool Update(Orders model)
        {
            //计算订单总金额:商品总金额+配送费用+支付手续费
            model.order_amount = model.real_amount + model.express_fee + model.payment_fee + model.invoice_taxes;
            return _repository.Update(model)>0;
        }

        public int Add(Orders model)
        {
            model.id = (int)_repository.Insert(model);
            #region 订单商品列表======================
            if (model.order_goods != null)
            {
                Order_goods goods = new Order_goods();
                foreach (Order_goods modelt in model.order_goods)
                {
                    goods = modelt;
                    goods.order_id = model.id;
                    _order_goods.Insert(goods);
                    //扣减商品库存
                    if (modelt.goods_id > 0)
                    {
                        var article_goods = _article_goods.GetList("where id=@goods_id and article_id=@article_id", new { goods_id = modelt.goods_id, article_id = modelt.article_id }).FirstOrDefault();
                        article_goods.stock_quantity-= modelt.quantity;
                        _article_goods.Update(article_goods);

                        int sum = _article_goods.GetList("where article_id=@article_id", new { article_id = modelt.article_id }).Sum(a => a.stock_quantity);
                        var article = _article.GetList("where id=@id", new { id = modelt.article_id }).FirstOrDefault();
                        article.stock_quantity = sum;
                        _article.Update(article);
                    }
                    else
                    {
                        var article = _article.GetList("where id=@id", new { id = modelt.article_id }).FirstOrDefault();
                        article.stock_quantity -= modelt.quantity;
                        _article.Update(article);
                    }
                }
            }
            #endregion
            return model.id;
        }

        public bool UpdateField(int id, string strValue)
        {
            return _repository.UpdateField(id, strValue);
        }

        public bool UpdateField(string order_no, string strValue)
        {
            return _repository.UpdateField(order_no, strValue);
        }
        /// <summary>
        /// 返回订单商品列表
        /// </summary>
        /// <param name="order_id">订单</param>
        /// <returns>List</returns>
        public List<Models.Article> get_order_goods_list(int order_id)
        {
            Models.Orders model = this.GetModel(order_id);
            if (model == null)
            {
                return null;
            }
            List<Models.Article> ls = new List<Models.Article>();
            if (model.order_goods != null)
            {
                foreach (Models.Order_goods modelt in model.order_goods)
                {
                    Models.Article goodsModel = _article.Get(modelt.article_id);
                    if (goodsModel != null)
                    {
                        ls.Add(goodsModel);
                    }
                }
            }
            return ls;
        }

        /// <summary>
        /// 返回订单状态
        /// </summary>
        /// <param name="_id">订单ID</param>
        /// <returns>String</returns>
        public string get_order_status(int _id)
        {
            string _title = "";
            Models.Orders model = this.GetModel(_id);
            switch (model.status)
            {
                case 1: //如果是线下支付，支付状态为0，如果是线上支付，支付成功后会自动改变订单状态为已确认
                    if (model.payment_status > 0)
                    {
                        _title = "待付款";
                    }
                    else
                    {
                        _title = "待确认";
                    }
                    break;
                case 2: //如果订单为已确认状态，则进入发货状态
                    if (model.express_status > 1)
                    {
                        _title = "已发货";
                    }
                    else
                    {
                        _title = "待发货";
                    }
                    break;
                case 3:
                    _title = "交易完成";
                    break;
                case 4:
                    _title = "已取消";
                    break;
                case 5:
                    _title = "已作废";
                    break;
            }

            return _title;
        }

        /// <summary>
        /// 返回订单是否需要在线支付
        /// </summary>
        /// <param name="order_id">订单ID</param>
        /// <returns>bool</returns>
        public bool get_order_payment_status(int order_id)
        {
            Models.Orders model = this.GetModel(order_id);
            if (model == null)
            {
                return false;
            }
            if (model.status != 1)
            {
                //return false;
            }
            Models.Payment payModel = _site_payment.GetPaymentModel(model.payment_id);
            if (payModel == null)
            {
                return false;
            }
            if (payModel.type == 1 && model.payment_status == 1)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 返回税金费用金额
        /// </summary>
        /// <param name="total_amount">总金额</param>
        /// <returns>decimal</returns>
        public decimal get_order_taxamount(decimal total_amount,Models.Orderconfig model)
        {
            decimal taxamount = model.taxamount;
            if (model.taxtype == 1)
            {
                taxamount = (taxamount * total_amount) / 100;
            }
            return taxamount;
        }
    }
}